package com.suxin.data.filter;

import com.alibaba.fastjson.JSONObject;
import com.suxin.data.consumer.DataConsumer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tang
 * @classname RestServer
 * @description [  ]
 * @date 2021/12/2 17:30
 */
@RestController
@RequestMapping("/rest")
public class RestServer implements InitializingBean {

    @Resource
    List<DataConsumer<JSONObject>> dataConsumers;

    @Resource
    List<DataFilter> dataFilters;

    @Resource
    DefaultDataFilterChain dataFilterChain;

    @GetMapping("/index")
    public Object index(@RequestBody JSONObject jsonObject) {
        if (dataFilterChain != null) {
            jsonObject = (JSONObject) dataFilterChain.doFilterChain(jsonObject);
        }
        JSONObject finalJsonObject = jsonObject;
        dataConsumers.forEach(consumer -> {
            consumer.consumer(finalJsonObject);
        });
        return new Object();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DefaultDataFilterChain dataFilterChain = new DefaultDataFilterChain(dataFilters);

    }
}